package com.greenleaf.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenleaf.entity.PdtDetail;
import com.greenleaf.entity.ShoppingCate;
import com.greenleaf.entity.Vip;
import com.greenleaf.service.PdtDetailService;
import com.greenleaf.service.ShoppingCateService;

@Controller
@RequestMapping("/pdtDetail")
public class PdtDetailController {
	@Resource
	private PdtDetailService pdtDetailService;
	@Resource
	private ShoppingCateService shoppingCateService;
	@RequestMapping("/get")
	@ResponseBody
//http://localhost:8080/Greenleaf/pdtDetail/get
	public Object getAllVip(HttpServletRequest request) {
		Vip vip=(Vip) request.getSession().getAttribute("vipInfo");
		ShoppingCate shoppingCate= shoppingCateService.getShoppingCate(vip.getvId());
		List<PdtDetail> pdtDetail = pdtDetailService.getPdtDetail(shoppingCate.getCateId());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count", pdtDetailService.getTotalPdtDetail(shoppingCate.getCateId()));
		map.put("data", pdtDetail);
		return map;
	}
	
	@RequestMapping("/gets")
	@ResponseBody
//http://localhost:8080/Greenleaf/pdtDetail/gets
	public Object getPdtDetail(HttpServletRequest request,String ids) {
		List<Integer> list = new ArrayList<Integer>();
		for (String id : ids.split(",")) {
			list.add(Integer.parseInt(id));
		}
		List<PdtDetail> pdtDetail = pdtDetailService.getPdtDetailByIds(list);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count", 0);
		map.put("data", pdtDetail);
		return map;
	}
	
//http://localhost:8080/Greenleaf/pdtDetail/addCart
	@RequestMapping("/addCart")
	@ResponseBody
	public Object addCart(HttpServletRequest request, PdtDetail pdtDetail) {
		/**dId=null, pdtId=1, pdtName=水晶花烛, pdtImg=images/product-item-20.jpg, unit=65.0, number=1, total=65.0, cateId=6, pdtUrl=http://localhost:8080/Greenleaf/productdetails-fullwidth.jsp?pdtId=1
		 * 1、先获取用户id 2、根据用户id获取购物车id 添加商品明显 (注意： a、先通过商品id判断该购物车是否有这个商品，如果有就更新数量和小计金额
		 * 如果没有，就直接添加 b、小计金额需要计算)
		 */
		
		Vip vip = (Vip) request.getSession().getAttribute("vipInfo");
		ShoppingCate cart = shoppingCateService.getShoppingCate(vip.getvId());
		pdtDetail.setCateId(cart.getCateId());
		pdtDetail.setTotal(pdtDetail.getNumber()*pdtDetail.getUnit());
		// 通过商品id和购物车id获取商品明显
		PdtDetail rs = pdtDetailService.getPdtDetailBypdt(pdtDetail);
		if (rs == null) {
			int r = pdtDetailService.addPdtDetail(pdtDetail);
				return r==1?true:false;
		} else {
			PdtDetail detail = new PdtDetail(rs.getdId(), null, null, null, pdtDetail.getUnit(), rs.getNumber()+pdtDetail.getNumber() ,rs.getTotal()+pdtDetail.getTotal(), null, null,null);
			int r = pdtDetailService.updatePdtDetail(detail);
			return r==1?true:false;
		}
	}
@RequestMapping("/updateNumById")
@ResponseBody
public Object updateNumById(PdtDetail pdtDetail) {
	
	int r = pdtDetailService.updatePdtDetail(pdtDetail);
	return r==1?true:false;
}

@RequestMapping("/del")
@ResponseBody
//http://localhost:8080/Greenleaf/pdtDetail/del
public Object delPdtDetail(Integer dId) {
  int r=pdtDetailService.delPdtDetail(dId);
  
	  return r==1?true:false;
  
}
}
